/**
 * 
 */
package spring.webapp.anmeldesystem.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.encoding.Md5PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import spring.webapp.anmeldesystem.entity.Admin;
import spring.webapp.anmeldesystem.entity.Professor;
import spring.webapp.anmeldesystem.entity.Student;
import spring.webapp.anmeldesystem.entity.User;
import spring.webapp.anmeldesystem.persistence.dao.AdminDao;
import spring.webapp.anmeldesystem.persistence.dao.ProfessorDao;
import spring.webapp.anmeldesystem.persistence.dao.StudentDao;
import spring.webapp.anmeldesystem.service.AdminService;

/**
 * @author Songhua Cui
 * 
 */
@Service
@Transactional
public class AdminServiceImpl implements AdminService {

	private static final String STUDENT = "student";
	private static final String PROFESSOR = "professor";
	private static final String ADMIN = "admin";

	@Autowired
	private AdminDao adminDao;
	@Autowired
	private ProfessorDao professorDao;
	@Autowired
	private StudentDao studentDao;

	@Override
	public void addStudent(Student Student) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addProfessor(Professor Professor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateProfessor(Professor Professor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void updateStudent(Student Student) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resetStudent(Student Student) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resetProfessor(Professor Professor) {
		// TODO Auto-generated method stub

	}

	@Override
	public void addPassword(long id) {
		// TODO Auto-generated method stub

	}

	@Override
	public void resetPassword(long id) {
		// TODO Auto-generated method stub

	}

	@Override
	public Admin getAdminInformation(String email) {
		return adminDao.getAdminByEmail(email);
	}

	@Override
	public List<User> getAllUsers() {
		return adminDao.getAllUsers();
	}

	@Override
	public List<User> getUserByEmail(String email) {
		List<User> users = new ArrayList<User>();

		Student student = studentDao.getStudentbyEmail(email);
		if (student != null) {
			users.add(student);
		} else {
			Professor professor = professorDao.getProfessorbyEmail(email);
			if (professor != null) {
				users.add(professor);
			} else {
				Admin admin = adminDao.getAdminByEmail(email);
				if (admin != null) {
					users.add(admin);
				}
			}
		}
		return users;
	}

	@Override
	public void deleteUser(String email) {
		Student student = studentDao.getStudentbyEmail(email);
		if (student != null) {
			studentDao.deleteStudent(student.getId());
		} else {
			Professor professor = professorDao.getProfessorbyEmail(email);
			if (professor != null) {
				professorDao.deleteProfessor(professor.getId());
			} else {
				Admin admin = adminDao.getAdminByEmail(email);
				if (admin != null) {
					adminDao.deleteAdmin(email);
				}
			}
		}
	}

	@Override
	public void resetPassword(String email) {

		Md5PasswordEncoder md5PasswordEncoder = new Md5PasswordEncoder();
		String encodePassword = md5PasswordEncoder.encodePassword("abcd", null);

		Student student = studentDao.getStudentbyEmail(email);
		if (student != null) {
			student.setKennwort(encodePassword);
			studentDao.resetPassword(student);
		} else {
			Professor professor = professorDao.getProfessorbyEmail(email);
			if (professor != null) {
				professor.setKennwort(encodePassword);
				professorDao.resetPassword(professor);
			} else {
				Admin admin = adminDao.getAdminByEmail(email);
				if (admin != null) {
					admin.setKennwort(encodePassword);
					adminDao.resetPassword(admin);
				}
			}
		}

	}

	@Override
	public String addNewUser(String role, String name, String faculity) {
		StringBuilder message = new StringBuilder();
		Md5PasswordEncoder md5PasswordEncoder = new Md5PasswordEncoder();
		String encodePassword = md5PasswordEncoder.encodePassword("abcd", null);
		String email = createEmailFromName(name);
		if (STUDENT.equalsIgnoreCase(role)) {
			if (studentDao.getStudentbyEmail(email) == null) {
				Student student = new Student();
				student.setName(name);
				student.setSemesterNummber(1);
				student.setEmail(email);
				student.setKennwort(encodePassword);
				adminDao.addStudent(student, faculity);
				message.append("Ein neuer Benutzer ");
				message.append(student.getName() + " mit Email "
						+ student.getEmail());
				message.append(" wurde erfolgreich " + "angelegt");
			} else {
				message.append("User existert schon im System!");
			}
		} else if (PROFESSOR.equalsIgnoreCase(role)) {
			if (professorDao.getProfessorbyEmail(email) == null) {
				Professor professor = new Professor();
				professor.setName(name);
				professor.setEmail(email);
				professor.setKennwort(encodePassword);
				adminDao.addProfessor(professor, faculity);
				message.append("Ein neuer Benutzer ");
				message.append(professor.getName() + " mit Email "
						+ professor.getEmail());
				message.append(" wurde erfolgreich " + "angelegt");
			} else {
				message.append("User existert schon im System!");
			}
		} else if (ADMIN.equalsIgnoreCase(role)) {
			if (adminDao.getAdminByEmail(email) == null) {
				Admin admin = new Admin();
				admin.setName(name);
				admin.setEmail(email);
				admin.setKennwort(encodePassword);
				adminDao.addAdmin(admin);
				message.append("Ein neuer Benutzer ");
				message.append(admin.getName() + " mit Email "
						+ admin.getEmail());
				message.append(" wurde erfolgreich " + "angelegt");
			} else {
				message.append("User existert schon im System!");
			}
		}
		return message.toString();
	}

	private String createEmailFromName(String name) {
		String suffixOfEmail = "@w-hs.de";
		String prefixOFEmail = StringUtils.deleteWhitespace(name);
		return StringUtils.lowerCase(prefixOFEmail + suffixOfEmail);
	}

	@Override
	public String updatePassword(String email, String oldPassword,
			String newPassowrd) {
		String message = "";

		Student student = studentDao.getStudentbyEmail(email);
		Md5PasswordEncoder md5PasswordEncoder = new Md5PasswordEncoder();
		String encodePassword = md5PasswordEncoder.encodePassword(oldPassword,
				null);
		if (student != null) {
			if (student.getKennwort().equalsIgnoreCase(encodePassword)) {
				student.setKennwort(md5PasswordEncoder.encodePassword(
						newPassowrd, null));
				studentDao.resetPassword(student);
				message = "Kennwort wurde erfolgreich aktualisiert.";
			} else {
				message = "Das eingegebenes altes Kennwort ist nicht korrekt.";
			}
		} else {
			Professor professor = professorDao.getProfessorbyEmail(email);
			if (professor != null) {
				if (professor.getKennwort().equalsIgnoreCase(encodePassword)) {
					professor.setKennwort(md5PasswordEncoder.encodePassword(
							newPassowrd, null));
					professorDao.resetPassword(professor);
					message = "Kennwort wurde erfolgreich aktualisiert.";
				} else {
					message = "Das eingegebenes altes Kennwort ist nicht korrekt.";
				}
			}
			// else {
			// Admin admin = adminDao.getAdminByEmail(email)(userId);
			// if (admin != null) {
			// adminDao.deleteAdmin(email);
			// }
			// }
		}
		return message;
	}
}
